// pages/shop_admission/index.js
var app = getApp();
var md5 = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agreement: false,
    buttone: false,
    shop_add_money:0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //检测是否支付
    var service = 'Shop.CheckShopOrder'
    var user_id = wx.getStorageSync('user_id');
    var openid = wx.getStorageSync('openid');
    var sign = md5.md5(openid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        openid: openid
      },
      success: function (res) {
        if (res.data.data.code == 0){
          wx.navigateTo({
            url: "/pages/shop_industry/index"
          });
        }
      }
    })
    that.getMoney()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
   /**
   * 提交入驻订单
   */
  bindSave: function (e) {
    var that = this;
    var shop_name = e.detail.value.shop_name;
    var shop_user_name = e.detail.value.shop_user_name;
    var shop_tel = e.detail.value.shop_tel;
    var shop_address = e.detail.value.shop_address;
    var shop_address_jingwei = wx.getStorageSync('latitude') + ',' + wx.getStorageSync('longitude')
    if (shop_name == "") {
      wx.showModal({
        title: '错误',
        content: '请填写商家名称',
        showCancel: false
      })
      return
    }
    if (shop_user_name == "") {
      wx.showModal({
        title: '错误',
        content: '请填写店主姓名',
        showCancel: false
      })
      return
    }
    if (shop_tel == "") {
      wx.showModal({
        title: '错误',
        content: '请填写联系电话',
        showCancel: false
      })
      return
    }
    if (shop_address == "") {
      wx.showModal({
        title: '错误',
        content: '请填写店铺地址',
        showCancel: false
      })
      return
    }
    var user_id = wx.getStorageSync('user_id');
    var openid = wx.getStorageSync('openid');
    var service = 'Shop.MakeShopOrder';
    var sign = md5.md5(openid + service + shop_address + shop_address_jingwei + shop_name + shop_tel + shop_user_name + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        openid: openid,
        shop_name: shop_name,
        shop_user_name: shop_user_name,
        shop_tel: shop_tel,
        shop_address: shop_address,
        shop_address_jingwei: shop_address_jingwei
      },
      success: function (res) {
        if (res.data.data.code == 0){
          wx.setStorage({ key: 'shop_order_id', data: res.data.data.info.order_id })
          wx.setStorage({ key: 'shop_order_num', data: res.data.data.info.order_num })
          wx.navigateTo({
            url: "/pages/shop_industry/index"
          });
        }
      }
    })
  },
  /**
   * 查询入驻价格
   */
  getMoney: function(){
    var that = this;
    var service = 'Shop.AddShopMoney'
    var user_id = wx.getStorageSync('user_id');
    var openid = wx.getStorageSync('openid');
    var sign = md5.md5(openid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        openid: openid
      },
      success: function (res) {
        that.setData({
          shop_add_money: res.data.data.info.shop_add_money
        });
      }
    })
  },
  /**
   * 点击激活按钮
   */
  checkboxChange: function (e) {
    if (e.detail.value[0] == 1){
      console.log(e.detail.value[0])
      this.setData({
        buttone: true
      })  
    }else{
      this.setData({
        buttone: false
      })  
    }
    console.log(this.data)
  },
  /**
   * 点击
   */
  checkActivation: function (e) {
    if (e.currentTarget.dataset.id){
      this.setData({
        agreement: false
      }) 
    }else{
      this.setData({
        agreement: true
      }) 
    }
  }
})